package com.marvinlabs.widget.slideshow.adapter;

import com.marvinlabs.widget.slideshow.util.HeLog;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.net.NetHandle;
import ohos.net.NetManager;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;

/**
 * A GenericBitmapAdapter that loads images from the Internet.
 * <p/>
 * Created by Vincent Mimoun-Prat @ MarvinLabs on 29/05/2014.
 */
public abstract class GenericRemoteBitmapAdapter<T> extends GenericBitmapAdapter<T> {

    // Options for the BitmapFactory to decode the bitmap
    private ImageSource.DecodingOptions bitmapFactoryOptions;

    //==============================================================================================
    // GENERAL METHODS
    //==

    /**
     * Constructor
     *
     * @param context The context in which the adapter is created
     * @param items The items for which we have images to load
     */
    public GenericRemoteBitmapAdapter(Context context, Collection<T> items) {
        super(context, items);
    }

    /**
     * Constructor
     *
     * @param context The context in which the adapter is created
     * @param items The items for which we have images to load
     * @param bitmapFactoryOptions The options to pass to the BitmapFactory used to decode the
     * bitmaps
     */
    public GenericRemoteBitmapAdapter(Context context, Collection<T> items, ImageSource.DecodingOptions bitmapFactoryOptions) {
        super(context, items);
        this.bitmapFactoryOptions = bitmapFactoryOptions;
    }

    //==============================================================================================
    // ASYNC MANAGEMENT METHODS
    //==

    protected abstract String getItemImageUrl(T item, int position);

    @Override
    protected PixelMap asyncLoadBitmap(T item, int position) {
        InputStream in = null;
        try {
            URL url = new URL(getItemImageUrl(item, position));
            NetManager netManager = NetManager.getInstance(null);

            if (!netManager.hasDefaultNet()) {
                return null;
            }
            NetHandle netHandle = netManager.getDefaultNet();
            URLConnection urlConnection = netHandle.openConnection(url,
                    java.net.Proxy.NO_PROXY);

            HttpURLConnection connection = null;
            if (urlConnection instanceof HttpURLConnection) {
                connection = (HttpURLConnection) urlConnection;
            }
            connection.setRequestMethod("GET");
            connection.connect();

            in = connection.getInputStream();
            PixelMap bm = ImageSource.create(in, new ImageSource.SourceOptions()).createPixelmap(bitmapFactoryOptions);
            return bm;
        } catch (Exception e) {
            HeLog.e("RemoteImageSlide", "Error while downloading image slide");
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
            } catch (Exception e) { /* Ignored */ }
        }
        return null;
    }

}
